package ldh.fx.adminfx.commpont.grid;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import ldh.fx.adminfx.pojo.FxmlBean;
import ldh.fx.adminfx.util.RegionUtil;
import ldh.fx.adminfx.util.TableViewUtil;
import org.controlsfx.control.MaskerPane;
import org.controlsfx.control.MasterDetailPane;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;

import java.util.List;
import java.util.function.Function;

public class GridTable<D> extends StackPane implements LoadData {

    protected TableView<D> tableView = new TableView();

    protected MaskerPane maskerPane = new MaskerPane();
    protected FxmlBean<HBox, PageablePaneController> pageablePaneBean = RegionUtil.loadFxmlBean("/fxml/PageablePane.fxml");
    protected AnchorPane freezePane = new AnchorPane();
    protected HBox tablePane = new HBox();
    protected VBox container = new VBox();
//    private Region searchContainer;
//    private MasterDetailPane masterDetailPane = new MasterDetailPane(Side.TOP);
    protected Pageable pageable;
    protected Function<Pageable, PageResult<D>> loadFunction;

    public GridTable() {
        buildUi();
    }

    protected void buildUi() {
        this.getStyleClass().add("grid-table");
//        ToolBar toolBar = createToolBar();
        maskerPane.setVisible(false);
        maskerPane.setText("正在加载中。。。");
//        maskerPane.setPrefSize(200, 200);
//        maskerPane.setMaxSize(200, 200);
        createBottom();

//        VBox vbox = new VBox();
//        vbox.getChildren().addAll(tableView, pageablePane);
//        searchContainer = buildSearchContainer(masterDetailPane);
//        masterDetailPane.setShowDetailNode(false);
//        if (searchContainer != null) {
//            masterDetailPane.setDetailNode(searchContainer);
//        }
//        masterDetailPane.setMasterNode(vbox);
//        VBox.setVgrow(masterDetailPane, Priority.ALWAYS);

        freezePane.getChildren().add(tablePane);
        AnchorPane.setTopAnchor(tablePane, 0d);
        AnchorPane.setRightAnchor(tablePane, 0d);
        AnchorPane.setBottomAnchor(tablePane, 0d);
        AnchorPane.setLeftAnchor(tablePane, 0d);
        tablePane.getChildren().add(tableView);
        HBox.setHgrow(tableView, Priority.ALWAYS);
        container.getChildren().addAll(freezePane, pageablePaneBean.getNode());
        VBox.setVgrow(freezePane, Priority.ALWAYS);

        this.getChildren().addAll(container, maskerPane);

        tableView.getStyleClass().add("table");
    }

    public void alignment(Pos alignment) {
        TableViewUtil.alignment(alignment, tableView);
    }

    public final ObservableList<TableColumn<D,?>> getColumns() {
        return tableView.getColumns();
    }

    public void setPageSizes(List<Integer> pageSizes) {
        pageablePaneBean.getController().setPageSizes(pageSizes);
    }

    public void setDefaultPageSize(int pageSize) {
        pageablePaneBean.getController().setPageSize(pageSize);
    }

    public void setLoadFunction(Function<Pageable, PageResult<D>> loadFunction) {
        this.loadFunction = loadFunction;
    }

    public TableView<D> getTableView() {
        return tableView;
    }

    protected void createBottom() {
        pageablePaneBean.getController().setLoadData(this);
    }

    private ToolBar createToolBar() {
        ToolBar toolBar = new ToolBar();
        toolBar.getStyleClass().add("table-toolbar");
        createFunctionBtn(toolBar);
        Region region = new Region();
        HBox.setHgrow(region, Priority.ALWAYS);
        Glyph searchGraphic = Glyph.create( "FontAwesome|" + FontAwesome.Glyph.SEARCH.name()).size(18).color(Color.CORNFLOWERBLUE);
        Button searchBtn = new Button("", searchGraphic);
//        searchBtn.setOnAction(e->expandPane());
        toolBar.getItems().addAll(region, searchBtn);
        return toolBar;
    }

//    private void expandPane() {
//        masterDetailPane.setShowDetailNode(!masterDetailPane.isShowDetailNode());
//    }

    protected void createFunctionBtn(ToolBar toolBar) {
    }

    @Override
    public void load(Pageable pageable) {
        if (pageable == null) {
            pageable = this.pageable;
        } else {
            this.pageable = pageable;
        }
        Pageable searchPagination = pageable;
        maskerPane.setVisible(true);

        Task<Void> task = new Task() {
            @Override
            protected Void call() throws Exception {
                if (loadFunction != null) {
                    PageResult<D> pageResult = loadFunction.apply(searchPagination);
                    Platform.runLater(()->setData(pageResult));
                }
                return null;
            }
        };
        task.setOnSucceeded(event -> {loadingDataEnd(task);});
        task.setOnFailed(event->{loadingDataEnd(task);});
        new Thread(task).start();
    }

    private void loadingDataEnd(Task task) {
        maskerPane.textProperty().unbind();
        maskerPane.progressProperty().unbind();
        maskerPane.setVisible(false);
        if (task.getException() != null) {
            task.getException().printStackTrace();
        }
    }

    public void setData(PageResult pageResult) {
        if (pageResult != null) {
            tableView.getItems().clear();
            tableView.getItems().addAll(pageResult.getBeans());
            tableView.refresh();
            pageablePaneBean.getController().setPageResult(pageResult);
        }
    }

    private Region buildSearchContainer(MasterDetailPane masterDetailPane) {
        return null;
    }
}
